/*
 * @Author: liujm
 * @Date: 2022-11-14 15:27:59
 * @Description:
 */
import { groupBy } from 'lodash-es';
import SignthequoteParam from '../entities/SignthequoteParam';
import CommonService from '../../common/CommonService';
import SignthequoteResult from '../entities/SignthequoteResult';

/**
 * 签报service
 */

class SignthequoteService extends CommonService {
  /**
   * 获取表单信息，并且格式化表单内容
   * @param {} piid
   */
  getFormInfo = async (piid, obj) => {
    const data = await this.getFormCommonInfo(piid);
    const signthequoteResult = new SignthequoteResult(data, obj);
    const res = await signthequoteResult.getData();
    const {
      principal
    } = res;
    const principalPerson = principal ?? await this.getPrincipal(obj.processInstance?.draftUser?.identityId);
    return {
      ...res,
      principal: principalPerson
    };
  }

  /**
   * 保存表单信息
   * @param {*} data
   * @param {*} files
   * @param {*} isSaveFile 是否保存附件信息
   */
  saveFormInfo = async (data, files = [], isSaveFile = true) => {
    const signthequoteParam = new SignthequoteParam(data, files, isSaveFile);
    const { data: { data: id } } = await this.saveFormCommonInfo(signthequoteParam.setData());
    return id;
  }

  /**
   * 获取主送抄送数据
   * @param {string} processDefinition 流程模板code
   */
  getSendData = async (processDefinition = 'gswj') => {
    // FIXME: 配置完后修改
    const data = await this.getSendCommonData(processDefinition);
    const arrs = groupBy(data, 'name');
    const res = [{
      1: this.formatSendData(arrs['省公司'], 1, 1, 3),
      2: this.formatSendData(arrs['境外公司'], 1, 2, 1),
      3: this.formatSendData(arrs['集团部门'], 1, 3, 1),
      4: this.formatSendData(arrs['直属单位'], 1, 4, 1),
    }, {
      2: this.formatSendData(arrs['专业公司'], 2, 2, 2),
      3: this.formatSendData(arrs['IT公司'], 2, 3, 1),
      4: this.formatSendData(arrs['总部公司'], 3, 4, 1, false),
    }, {
      3: this.formatSendData(arrs['纪检'], 3, 3, 1),
      4: this.formatSendData(arrs['其它公司'], 3, 4, 1),
    }];
    return {
      // 数据
      data: res,
      // 每行列数
      cols: 4,
      // 行数
      rows: 3,
    };
  }
}

export default SignthequoteService;
